<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">确定</el-button>
    </div>

    <!-- 装饰项目代码 -->
    <BaseDialog :visible.sync="visibleProject" title="装饰项目代码" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <searchProjectCode @close="shelperVisible = false" @onConfirm="handleDealerConfirm" @close2="closeHandler2" @close3="closeHandler3" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import searchProjectCode from './searchProjectCode';
import { getWorkGroupData } from '@/api/part/qualityManagement/decorationDispatchOrder';

export default {
  name: 'decorProduct',
  components: {
    searchProjectCode
  },
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      visibleProject: false
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '装饰项目代码',
          fieldName: 'proCode',
          placeholder: '请输入...',
          unitRender: props => {
            return <el-button icon="el-icon-search" onClick={() => this.searchProjectCode()}></el-button>;
          },
          rules: [{ required: true, message: '请选择装饰项目代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '装饰项目名称',
          fieldName: 'proName',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入装饰项目名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '装饰项目工时费',
          fieldName: 'manhaurFee',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入装饰项目工时费', trigger: 'blur' }]
        },

        {
          type: 'SELECT',
          label: '班组',
          fieldName: 'bzCode',
          itemList: [],
          rules: [{ required: true, message: '请选择班组', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '装饰项目说明',
          fieldName: 'remark',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入装饰项目说明', trigger: 'blur' }]
        }
      ];
    },

    // 点击供应商选择搜索
    handleSelectSupplier() {
      this.shelperVisible = true;
    },
    // 供应商选择点击确定
    handleDealerConfirm(e) {
      this.dealerInfo = e;
    },
    closeHandler2(val, data) {
      this.formList.map(item => {
        item.initialValue = data[0][item.fieldName];
      });
      this.visibleProject = false;
    },
    closeHandler3(val, data) {
      this.visibleProject = false;
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    saveDialog() {
      this.saveData = null;
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.saveData.bzName = this.workGroupData.find(item => item.WORKGROUP_CODE == this.saveData.bzCode).WORKGROUP_NAME;
        this.$emit('close3', false, this.saveData);
      }
    },
    searchProjectCode() {
      this.visibleProject = true;
    },
    // 获取班组数据
    async getlist() {
      this.workGroupData = (await getWorkGroupData()).data;
      this.formList.find(x => x.fieldName === 'bzCode').itemList = this.workGroupData.map(x => ({ text: x.WORKGROUP_NAME, value: x.WORKGROUP_CODE }));
    },
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>

<style lang="less" scoped></style>
